package com.wgy.day41JDBC.执行DML语句;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/*
account表 执行DML语句: 删除一条记录 delete from...
*/
public class Demo04JDBC {
    public static void main(String[] args) {
        Connection conn=null;
        Statement stmt=null;

        try {//1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2.定义sql
            String sql="delete from account where id=3";
            //3.获取connection对象
            conn = DriverManager.getConnection("jdbc:mysql:///db3","root","root");
            //4.获取执行sql的对象statement
            stmt = conn.createStatement();
            //5.执行sql,接收返回值结果
            int count = stmt.executeUpdate(sql);//count:影响的行数
            //6.处理结果
            System.out.println(count);
            if(count>0){
                System.out.println("删除成功!");
            }else{
                System.out.println("删除失败!");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }  //7.释放资源
        finally{
            // stmt.close();
            //避免空指针异常
            if(stmt!=null){
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }

}
